<?php

class AcaoService extends Model {

    /**
     * @var AcaoDao
     */
    private $acaoDao;
    /**
     * @var ControleAcaoService
     */
    private $controleAcaoService;

    public function injectDependencies() {
        $this->acaoDao = AcaoDao::getInstance();
        $this->controleAcaoService = ControleAcaoService::getInstance();
    }

    public function findAll() {
        $ArActions = $this->acaoDao->findAll();
        $this->fillAction($ArActions);
        return $ArActions;
    }

    public function findAllRestrictActions() {
        $ArActions = $this->acaoDao->findRestrictActions();
        $this->fillAction($ArActions);
        return $ArActions;
    }

    public function findFreeActions() {
        $ArActions = $this->acaoDao->findFreeActions();
        $this->fillAction($ArActions);
        return $ArActions;
    }

    public function update($ArNewActions, $IDPerfil) {
        $ArCurrentActions = $this->findByProfile($IDPerfil);
        $ArIdToUpdate = $this->getIdToUpdateActions($ArCurrentActions, $ArNewActions);
        $BoReturnInsert = $this->acaoDao->insertActionsFromProfile($ArIdToUpdate['Insert'], $IDPerfil);
        $BoReturnDelete = $this->acaoDao->deleteActionsFromProfile($ArIdToUpdate['Delete'], $IDPerfil);
        return $BoReturnInsert && $BoReturnDelete;
    }

    public function findByProfile($IDPerfil) {
        return $this->acaoDao->findByProfile($IDPerfil);
    }

    private function getEnabledActions($ArCurrentActions, $ArNewActions) {
        $ArEnabledActions = array();
        if( !empty($ArNewActions) ) {
            foreach($ArNewActions as $IDAction) {
                if( $this->isEnabled($ArCurrentActions, $IDAction) ) {
                    $ArEnabledActions[] = $IDAction;
                }
            }
        }
        return $ArEnabledActions;
    }

    private function getDisabledActions($ArCurrentActions, $ArNewActions) {
        $ArDisabledAction = array();
        if( !empty($ArCurrentActions) ) {
            foreach($ArCurrentActions as $Action) {
               if(!in_array($Action->getIDAcao(), $ArNewActions) ) {
                   $ArDisabledAction[] = $Action->getIDAcao();
                }
            }
        }
        return $ArDisabledAction;
    }

    private function isEnabled($ArCurrentActions, $IDAction) {
        if( !empty($ArCurrentActions) ) {
            foreach($ArCurrentActions as $Action) {
                if($Action->getIDAcao() == $IDAction) {
                    return false;
                }
            }
        }
        return true;
    }

    public function getIdToUpdateActions($ArCurrentActions, $ArNewActions) {
        return array(
            'Insert' => $this->getEnabledActions($ArCurrentActions, $ArNewActions),
            'Delete' => $this->getDisabledActions($ArCurrentActions, $ArNewActions)
        );
    }

    private function fillAction(&$MxActions) {
        if( !empty($MxActions) ) {
            $this->fillActionWithControlAccess($MxActions);
        }
    }

    private function fillActionWithControlAccess($MxActions) {
        if(!is_array($MxActions)) {
            $MxActions->setArControleAcao( $this->controleAcaoService->findByAction($MxActions->getIDAcao()) );
            return;
        }
         foreach($MxActions as &$Acao) {
            $Acao->setArControleAcao( $this->controleAcaoService->findByAction($Acao->getIDAcao()) );
        }
    }

}

?>
